package com.ruoyi.workflow.handler;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.toolkit.SimpleQuery;
import com.ruoyi.system.domain.SysUserPost;
import com.ruoyi.system.domain.SysUserRole;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.workflow.untils.JudgeConstant;
import lombok.AllArgsConstructor;
import org.flowable.engine.delegate.DelegateExecution;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

/**
 * 自定义处理类
 *
 * @author KonBAI
 */
@AllArgsConstructor
@Component("improveHandler")
public class ImproveHandler {
    @Resource
    ISysUserService sysUserService;
    public Set<String> getManagerIds(DelegateExecution execution) {
        //获取持续改善类型，类型 = 主管岗位Id
        String type = execution.getVariable("type").toString();
        // 通过主管岗位Id，获取所有用户Id集合
        LambdaQueryWrapper<SysUserPost> lqw = Wrappers.lambdaQuery(SysUserPost.class).select(SysUserPost::getUserId).in(SysUserPost::getPostId, type);
        List<Long> userIds = SimpleQuery.list(lqw, SysUserPost::getUserId);
        Set<String> candidateUserIds = new LinkedHashSet<>();
        // 添加候选用户Id
        userIds.forEach(id -> candidateUserIds.add(String.valueOf(id)));
        return candidateUserIds;
    }

    public Set<String> getJudgesIds(DelegateExecution execution) {
        //获取持续改善类型，类型 = 主管岗位Id
        String type = execution.getVariable("type").toString();
        //主管岗位Id转为评审团角色Id
        String roleId = JudgeConstant.getRoleId(type);
        // 通过角色Id，获取所有用户Id集合
        LambdaQueryWrapper<SysUserRole> lqw = Wrappers.lambdaQuery(SysUserRole.class).select(SysUserRole::getUserId).in(SysUserRole::getRoleId, roleId);
        List<Long> userIds = SimpleQuery.list(lqw, SysUserRole::getUserId);
        Set<String> candidateUserIds = new LinkedHashSet<>();
        // 添加候选用户Id
        userIds.forEach(id -> candidateUserIds.add(String.valueOf(id)));
        return candidateUserIds;
    }
}
